/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.action;

import model.database.SqlBook;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.entities.Book;
import model.entities.Category;

/**
 *
 * @author HoangTien
 */
public class CategoryAction implements Action {

    public void exe(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String url = "/direction.jsp";
        HttpSession session = request.getSession();
        if (session.getAttribute("Categories") == null) {
            request.getRequestDispatcher(url).forward(request, response); 
        } else {
            url = "/category.jsp";
            //SqlBook sqlBook = new SqlBook();
            int cateId = Integer.parseInt(request.getParameter("Id"));
            List<Book> listBooks = null;// = sqlBook.getListBooks(cateId); 
            List<Category> listCate = (List<Category>)session.getAttribute("Categories");
            for (Category cate : listCate) {
                if(cate.getCateId() == cateId) {
                    listBooks = cate.getListBooks();
                    session.setAttribute("BookByCategory", listBooks); 
                }
            } 
            request.getRequestDispatcher(url).forward(request, response);
        } 
    }
}
